package com.yksj.healthtalk.ui.doctorstation;

import java.util.List;

import org.json.JSONException;
import org.json.JSONObject;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.TextView;

import com.yksj.healthtalk.comm.BaseFragmentActivity;
import com.yksj.healthtalk.entity.DoctorServiceContentEntity;
import com.yksj.healthtalk.net.http.AsyncHttpResponseHandler;
import com.yksj.healthtalk.net.http.HttpRestClient;
import com.yksj.healthtalk.ui.R;
import com.yksj.healthtalk.utils.ToastUtil;
/**
 * 医生服务 添加服务内容
 * @author Administrator
 *
 */
public class DoctorServiceAddContent extends BaseFragmentActivity implements OnClickListener {
	private EditText mTextName;
	private EditText mContent;
	private DoctorServiceContentEntity entity;
	private String mType="addDoctorServler";//添加
	private String SERVICE_CONTENT_ID;
	private List<DoctorServiceContentEntity> mEntities;
	private  TextView content_conut;//字数限制
	private int mContentCount=500;//字数限制

	@Override
	protected void onCreate(Bundle arg0) {
		super.onCreate(arg0);
		setContentView(R.layout.doctor_service_add_content);
		initTitle();
		initView();
	}
	private void initView() {
		titleLeftBtn.setOnClickListener(this);
		titleTextV.setText("添加服务内容");
		titleRightBtn2.setOnClickListener(this);
		titleRightBtn2.setText("确定");
		titleRightBtn2.setVisibility(View.VISIBLE);
		mTextName = (EditText) findViewById(R.id.name);
		mContent = (EditText) findViewById(R.id.content);
		content_conut=(TextView)findViewById(R.id.content_conut);
		mContent.addTextChangedListener(new TextWatcher() {
			@Override
			public void onTextChanged(CharSequence s, int start, int before, int count) {
				 if((start +count)<=mContentCount && mContent.getText().toString().length()<=mContentCount){
		        		int textNumber = mContent.getText().toString().length();
		        		content_conut.setText(textNumber+"/"+mContentCount); 
		        	 }else{
		        		 mContent.setText(s.subSequence(0, mContentCount));
		        		 ToastUtil.showShort(DoctorServiceAddContent.this, "最多可输入"+mContentCount+"个字符");
		        	 }
			}
			
			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {
			}
			@Override
			public void afterTextChanged(Editable s) {
			}
		});
		
		if(getIntent().hasExtra("entity")){
			entity=(DoctorServiceContentEntity) getIntent().getSerializableExtra("entity");
			mTextName.setText(entity.getSERVICE_CONTENT());
			mContent.setText(entity.getSERVICE_DESC());
			SERVICE_CONTENT_ID=entity.getSERVICE_CONTENT_ID();
			mType="updateDoctorServler";
			mEntities = getIntent().getParcelableArrayListExtra("entitys");
		}
		
	}
	
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.title_back:
			onBackPressed();
			break;
		case R.id.title_right2:
			boolean isUpdate = true;
			if(!"".equals(mTextName.getText().toString())&&!"".equals(mContent.getText().toString())){
				if(mEntities!=null&&mType.equals("addDoctorServler")){
					for (int i = 0; i <mEntities.size(); i++) {
						if(mEntities.get(i).getSERVICE_CONTENT().equals(mTextName.getText().toString())){
							ToastUtil.showBasicShortToast(getApplicationContext(), "服务名称已存在。");
							isUpdate=false;
							 break;
						}
					}
					if(isUpdate)subMint();
				}else if(mEntities!=null&&mType.equals("updateDoctorServler")){
					for (int i = 0; i <mEntities.size(); i++) {
						if(mEntities.get(i).getSERVICE_CONTENT().equals(mTextName.getText().toString())&&(!mEntities.get(i).getSERVICE_CONTENT_ID().equals(entity.getSERVICE_CONTENT_ID()))){
							ToastUtil.showBasicShortToast(getApplicationContext(), "服务名称已存在。");
							isUpdate=false;
							 break; 
						}
					}
					if(isUpdate)subMint();
				}else{
					subMint();
				}
				
			}else{
				ToastUtil.showBasicShortToast(getApplicationContext(), "服务名称或内容不能为空。");
			}
			break;
		}
	}
	private void subMint() {
		HttpRestClient.doHttpCreateDoctorServiceContent(mType,SERVICE_CONTENT_ID,mTextName.getText().toString(),mContent.getText().toString(),new AsyncHttpResponseHandler(this){
			@Override
			public void onSuccess(int statusCode, String content) {
				super.onSuccess(statusCode, content);
				try {//{"SERVICE_CONTENT_ID":"45"}
					JSONObject object=new JSONObject(content);
					if(object.has("error_message")){
						ToastUtil.showBasicShortToast(getApplicationContext(), object.getString("error_message")+"");
					}else{
						Intent intent = getIntent();
						if(intent.getExtras().containsKey("array")){
							intent.putStringArrayListExtra("array", intent.getExtras().getStringArrayList("array"));
						}
						setResult(RESULT_OK, intent);
						finish();
					}
				} catch (JSONException e) {
				}
			}
		
		
		});
		
	}
}
